package ide.ccf.ccproject.wizards.pages;

import ide.ccf.ccproject.managedbuilder.MbsCustomPageManager;
import ide.ccf.ccproject.managedbuilder.MbsWizardHandler;
import ide.ccf.ccproject.translate.Message;

import java.util.Locale;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * Gui项目文件向导页面
 *
 */
public class FilesWizardPage extends WizardPage 
{
	public static final String PAGE_ID = "ide.ccf.ccproject.wizards.pages.FilesWizardPage"; 
	
	private boolean filechanged = false;
	
	private Text classLE;
	private Text cppLE;
	private Text hLE;
	private Text uiLE;
	private Combo uiType;

	/**
	 * 文件名变化侦听类
	 *
	 */
	class FileTextListener implements KeyListener 
	{
		public void keyPressed(KeyEvent e) {
		}

		public void keyReleased(KeyEvent e) 
		{
			filechanged = true;
			checkValidPage();
		}
	}

	/**
	 * 构造器
	 * @param Message.ModulesWizard_0
	 */
	public FilesWizardPage(MbsWizardHandler mbsWizardHandler) 
	{
		super(Message.FilesWizard_0);
		setDescription(Message.FilesWizard_2);
		setTitle(Message.FilesWizard_0);
		setWizard(mbsWizardHandler.getWizard());
		setPageComplete(false);
	}
	
	
	@Override
	public IWizardPage getNextPage() {
		return MbsCustomPageManager.getNextPage(PAGE_ID);
	}
	

	/**
	 * 返回合法类名
	 * @param name
	 * @return
	 */
	public static String toValidClassName(String name) 
	{
		String validName = "";

		char underscoreCandidates[] = {' ', '-'};
		
		for (int i = 0; i < underscoreCandidates.length; i++)
			name = name.replace(underscoreCandidates[i], '_');

		for (int i = 0; i < name.length(); i++) 
		{
			char character = name.charAt(i);
			
			boolean isAlpha = character >= 'a' && character <= 'z' || character >= 'A' && character <= 'Z';
			boolean isDigit = character >= '0' && character <= '9';
			boolean isUnderscore = character == '_';
			boolean isCharacterValid = (isAlpha || (isDigit && validName.length() > 0) || isUnderscore);
			
			validName += isCharacterValid ? character : '_';
		}

		return validName.length() == 0 ? "new_ccf_project" : validName;
	}

	/**
	 * 设置类名
	 * @param name
	 */
	public void setClassName(String name) 
	{
		if (classLE != null) 
		{
			String validName = toValidClassName(name);
			classLE.setText(validName);
			updateLEs(validName);
		}
	}

	/**
	 * 获取类名
	 * @return
	 */
	public String getClassName() 
	{
		return classLE.getText();
	}

	/**
	 * 获取源文件名
	 * @return
	 */
	public String getSourceFileName() 
	{
		return cppLE.getText();
	}

	/**
	 * 获取头文件名
	 * @return
	 */
	public String getHeaderFileName() 
	{
		return hLE.getText();
	}

	/**
	 * 获取UI文件名
	 * @return
	 */
	public String getUIFileName() 
	{
		return uiLE.getText();
	}

	/**
	 * 获取UI类名
	 * @return
	 */
	public String getUiClassName() 
	{
		//return uiType.getText();
		
		switch (uiType.getSelectionIndex())
		{
			case 0:
				return "QWidget";
				
			case 1:
				return "QMainWindow";
				
			case 2:
				return "QDialog";
				
			default:
				return "";
		}
	}

	/**
	 * 创建向导页面显示控制
	 */
	public void createControl(Composite parent) 
	{
		Composite mainComposite = new Composite(parent, SWT.NONE);

		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		mainComposite.setLayout(layout);

		Label cl = new Label(mainComposite, SWT.PUSH);
		cl.setText(Message.FilesWizard_4);

		classLE = new Text(mainComposite, SWT.BORDER | SWT.SINGLE);
		classLE.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));

		Label cppl = new Label(mainComposite, SWT.PUSH);
		cppl.setText(Message.FilesWizard_5);

		cppLE = new Text(mainComposite, SWT.BORDER | SWT.SINGLE);
		cppLE.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
		cppLE.addKeyListener(new FileTextListener());

		Label hl = new Label(mainComposite, SWT.PUSH);
		hl.setText(Message.FilesWizard_6);

		hLE = new Text(mainComposite, SWT.BORDER | SWT.SINGLE);
		hLE.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
		hLE.addKeyListener(new FileTextListener());

		Label uil = new Label(mainComposite, SWT.PUSH);
		uil.setText(Message.FilesWizard_7);

		uiLE = new Text(mainComposite, SWT.BORDER | SWT.SINGLE);
		uiLE.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
		uiLE.addKeyListener(new FileTextListener());

		Label uiTypeLable = new Label(mainComposite, SWT.PUSH);
		uiTypeLable.setText(Message.FilesWizard_8);

		uiType = new Combo(mainComposite, SWT.READ_ONLY);
		uiType.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
		uiType.setItems(new String[] { Message.FilesWizard_9, Message.FilesWizard_10, Message.FilesWizard_11 });
		uiType.select(0);

		classLE.addKeyListener(new KeyListener() 
		{
			public void keyPressed(KeyEvent e) 
			{
			}

			public void keyReleased(KeyEvent e) 
			{
				updateLEs(classLE.getText());
			}
		});
		setClassName(CCFMainWizardPage.getProjectName() == null ? "" : CCFMainWizardPage.getProjectName());
		setControl(mainComposite);
	}

	/**
	 * 更新文件名
	 * @param text
	 */
	private void updateLEs(String text) 
	{
		if(!text.isEmpty())
		{
			
			cppLE.setText(text.toLowerCase(Locale.US) + ".cpp");
			hLE.setText(text.toLowerCase(Locale.US) + ".h");
			uiLE.setText(text.toLowerCase(Locale.US) + ".ui");
			setPageComplete(true);
		}
		else
		{
			setPageComplete(false);
			cppLE.setText("");
			hLE.setText("");
			uiLE.setText("");
		}
	}

	/**
	 * 设置页面是否正确
	 */
	private void checkValidPage() 
	{
		Text[] text = { classLE, cppLE, hLE, uiLE };
		for (int i = 0; i < text.length; i++) 
		{
			String string = text[i].getText().trim();
			
			if (string.length() > 0 && string.indexOf(" \\/") > -1) 
			{ 
				setPageComplete(false);
				return;
			}
		}
		setPageComplete(true);
	}
}